package diva.test_server.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PapAndroidServiceImpl extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    try {
      String action = req.getParameter("action");
      String username = req.getParameter("username");
      String password = req.getParameter("password");
      String session = req.getParameter("session");
      if ("widget".equals(action)) {
        processWidgetRequest(out, username, password);
        return;
      }
      if ("login".equals(action)) {
        processLoginRequest(out, username, password);
        return;
      }
      if ("waiting_tasks".equals(action)) {
        processWaitingTasksRequest(out, session);
        return;
      }
      if ("load_transactions".equals(action)) {
        processTransactionsRequest(out, session);
        return;
      }
      if ("trafic_overview".equals(action)) {
        processTraficRequest(req, out, session);
        return;
      }
      if ("sell_notification".equals(action)) {
        processSellNotofocationRequest(req, out, session);
        return;
      }
    } catch (Exception e) {
      out.println("{exception:" + e.getMessage() + "}");
    }
  }

  private String duplicateJsonArrayItems(int count, String string) {
    String out = "";
    for (int i = 0; i < count; i++)
      out += string + ",";
    out = out.substring(0, out.length() - 1);
    return out;
  }

  private void processLoginRequest(PrintWriter out, String username, String password) {
    if ("merchant".equals(username) && "password".equals(password))
      out.println("{success:true,session:'12345',user_type:merchant}");
    else if ("affiliate".equals(username) && "password".equals(password))
      out.println("{success:true,session:'3456',user_type:affiliate}");
    else out.println("{success:false}");
  }

  private void processSellNotofocationRequest(HttpServletRequest req, PrintWriter out,
      String session) {
    if ("12345".equals(session))
      out.println("{success:true,wasSell:true}");
    else if ("3456".equals(session))
      out.println("{success:true,wasSell:false}");
    else out.println("{success:false}");
  }

  private void processTraficRequest(HttpServletRequest req, PrintWriter out, String session) {
    String period = req.getParameter("period");
    if ("12345".equals(session)) {
      if ("today".equals(period))
        out.println("{success:true,chargebacksapproved:2,chargebackspaid:2,"
            + "chargebackspending:42,clicksraw:1,clicksunique:32,"
            + "commisionsapproved:12,commisionspaid:12,commisionspending:1,"
            + "impressionsraw:12,impressionsunique:12,refundsapproved:2,"
            + "refundspaid:2,refundspending:12,salesapproved:10,"
            + "salescostapproved:72,salescostpaid:2,salescostpending:32,"
            + "salespaid:22,salespending:2}");
      else out.println("{success:true,chargebacksapproved:12,chargebackspaid:132,"
          + "chargebackspending:142,clicksraw:1,clicksunique:132,"
          + "commisionsapproved:212,commisionspaid:512,commisionspending:12,"
          + "impressionsraw:12,impressionsunique:612,refundsapproved:12,"
          + "refundspaid:142,refundspending:12,salesapproved:120,"
          + "salescostapproved:172,salescostpaid:182,salescostpending:132,"
          + "salespaid:122,salespending:192}");
    } else if ("3456".equals(session))
      if ("today".equals(period))
        out.println("{success:true,chargebacksapproved:12,chargebackspaid:132,"
            + "chargebackspending:1,clicksraw:1,clicksunique:2,"
            + "commisionsapproved:2,commisionspaid:5,commisionspending:12,"
            + "impressionsraw:12,impressionsunique:6,refundsapproved:1,"
            + "refundspaid:2,refundspending:12,salesapproved:0,"
            + "salescostapproved:1,salescostpaid:1,salescostpending:132,"
            + "salespaid:2,salespending:1}");
      else out.println("{success:true,chargebacksapproved:12,chargebackspaid:132,"
          + "chargebackspending:142,clicksraw:1,clicksunique:132,"
          + "commisionsapproved:212,commisionspaid:512,commisionspending:12,"
          + "impressionsraw:12,impressionsunique:612,refundsapproved:12,"
          + "refundspaid:142,refundspending:12,salesapproved:120,"
          + "salescostapproved:172,salescostpaid:182,salescostpending:132,"
          + "salespaid:122,salespending:192}");
    else out.println("{success:false}");
  }

  private void processTransactionsRequest(PrintWriter out, String session) {
    if ("12345".equals(session))
      out.println("{success:true,rows:["
          + duplicateJsonArrayItems(30,
              "{affiliate_name:CharlesBukowski,campaign_name:SecondCampaign,"
                  + "commision:$135,created:7868967,order_id:OrderId,"
                  + "product_id:ProductId,status:D}") + "],all_count:50 }");
    if ("3456".equals(session))
      out.println("{success:true,rows:["
          + duplicateJsonArrayItems(30, "{campaign_name:FirstCampaigne,commision:$88,"
              + "created:65785675,order_id:OrderId,product_id:ProductId,status:A}")
          + "],all_count:50}");
    else out.println("{success:false}");
  }

  private void processWaitingTasksRequest(PrintWriter out, String session) {
    if ("12345".equals(session))
      out.println("{success:true,pending_affiliates:53,pending_commisions:530,pending_links:3}");
    else out.println("{success:false}");
  }

  private void processWidgetRequest(PrintWriter out, String username, String password) {
    if ("merchant".equals(username) && "password".equals(password))
      out.println("{success:true,clicks:111,transactions:2221}");
    else if ("affiliate".equals(username) && "password".equals(password))
      out.println("{success:true,clicks:50,transactions:11}");
    else out.println("{success:false}");
  }
}
